/*
 * Copyright 2012 Marsrutai.lt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import lt.marsrutai.mobile.android.model.ArrivalTime;
import lt.marsrutai.mobile.android.model.enums.DateTime;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

public class NextDepartures {

    private String returnString = "??";

    public NextDepartures(List<ArrivalTime> trackStopTimes) {
        this(trackStopTimes, false);
    }

    public NextDepartures(List<ArrivalTime> trackStopTimes, boolean expected) {

        List<String> stopTimes = new ArrayList<String>();
        if (trackStopTimes != null) {
            for (ArrivalTime stopTime: trackStopTimes) {
                Calendar time = expected ? stopTime.getExpectedTime() : stopTime.getScheduledTime();

                if (stopTime.getExpectedTime().equals(stopTime.getScheduledTime())) {
                    stopTimes.add(DateTime.formatHourMinuteRounded(time)); // Scheduled
                } else
                {
                    stopTimes.add("(" + DateTime.formatHourMinuteRounded(time) + ")"); // Real-time: mark for highlighting
                }
            }
        }

        returnString = StringUtils.join(stopTimes, ", ");
    }

    /**
     * Apply text with styling
     * @param textView
     */
    public static void setColoredText(TextView textView, String value, int color) {
        List<Integer> startPos = new ArrayList<Integer>();
        List<Integer> endPos = new ArrayList<Integer>();

        // Collect positions of highlights, indicated by brackets
        while (value.indexOf("(") >= 0 && value.indexOf(")") >= value.indexOf("(")) {
            int pos = value.indexOf("(");
            startPos.add(pos);
            value = value.substring(0, pos) + value.substring(pos + 1);
            pos = value.indexOf(")");
            endPos.add(pos);
            value = value.substring(0, pos) + value.substring(pos + 1);
        }

        textView.setText(value, TextView.BufferType.SPANNABLE);
        Spannable spn = (Spannable)textView.getText();

        for (int i = 0; i < startPos.size(); i++) {
            spn.setSpan(new ForegroundColorSpan(color), startPos.get(i), endPos.get(i),  Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }

    @Override
    public String toString() {
        return returnString;
    }

}
